package com.yhg.qxmall.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yhg.qxmall.member.entity.MemberReceiveAddressEntity;
import com.yhg.qxmall.member.service.MemberReceiveAddressService;
import com.yhg.common.utils.PageUtils;
import com.yhg.common.utils.R;



/**
 * 会员收货地址
 *
 * @author 凌月青缃
 * @email linyueqingxiang@gmail.com
 * @date 2020-07-21 23:21:55
 */
@RestController
@RequestMapping("member/memberreceiveaddress")
public class MemberReceiveAddressController {
    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;

/****************     ===========  MQ版订单服务    ===========  ******************/
//    @GetMapping("/")
//    public R getMemberAddress


/****************     ===========  MQ版订单服务    ===========  ******************/


    @GetMapping("/{memberId}/addresses")
    public List<MemberReceiveAddressEntity> getAddress(@PathVariable("memberId") Long memberId){

        return memberReceiveAddressService.getAddress(memberId);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberReceiveAddressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberReceiveAddressEntity memberReceiveAddress = memberReceiveAddressService.getById(id);

        return R.ok().put("memberReceiveAddress", memberReceiveAddress);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody MemberReceiveAddressEntity memberReceiveAddress){

        if (memberReceiveAddress.getDefaultStatus()==1){
            List<MemberReceiveAddressEntity> memberAddressS = memberReceiveAddressService.list(new QueryWrapper<MemberReceiveAddressEntity>()
                    .eq("member_id", memberReceiveAddress.getMemberId()));
            MemberReceiveAddressEntity addressEntity = new MemberReceiveAddressEntity();
            for (int i = 0; i < memberAddressS.size(); i++) {
                addressEntity = memberAddressS.get(i);
                addressEntity.setDefaultStatus(0);
                memberReceiveAddressService.updateById(addressEntity);
            }
            memberReceiveAddressService.save(memberReceiveAddress);
        }else {
            memberReceiveAddressService.save(memberReceiveAddress);
        }
        return R.ok().put("msg","保存成功！");
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody MemberReceiveAddressEntity memberReceiveAddress){
        if (memberReceiveAddress.getDefaultStatus()==1){
            //先改一下
            memberReceiveAddressService.updateById(memberReceiveAddress);
            //检索出全部
            List<MemberReceiveAddressEntity> memberAddressS = memberReceiveAddressService.list(new QueryWrapper<MemberReceiveAddressEntity>()
                    .eq("member_id", memberReceiveAddress.getMemberId()));
            MemberReceiveAddressEntity addressEntity = new MemberReceiveAddressEntity();
            for (int i = 0; i < memberAddressS.size(); i++) {
                //取行
                addressEntity = memberAddressS.get(i);
                //如果找到自己的收获地址，则存1   置其它为零
                if (addressEntity.getId()==memberReceiveAddress.getId()){
                    addressEntity.setDefaultStatus(1);
                }else addressEntity.setDefaultStatus(0);
                //更新
                memberReceiveAddressService.updateById(addressEntity);
            }
        }else {//若本来为0
            MemberReceiveAddressEntity addressEntity = new MemberReceiveAddressEntity();
            BeanUtils.copyProperties(memberReceiveAddress,addressEntity);
            memberReceiveAddressService.saveOrUpdate(addressEntity);
        }
        return R.ok().put("msg","修改成功！");
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberReceiveAddressService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
